#!/usr/bin/python
r''' 

``base16decode.py``

    :date: 20. Okt. 2011

'''


def base16decode(input_data):
    '''
decode base16 presentation to data

:param input_data: the base 16 presentation of data (type ``str``)
:return: the raw data
:rtype: ``str``
    '''
    
    base = "0123456789ABCDEF"
    __all__ =""
    decode=""
    for i in range(len(input_data)):
        input_value = input_data[i]
        
        for j in range(len(base)):
            if input_value == base[j]:
                value = j
                binary = bin(value)
                convert = binary.lstrip('0b')
                
                if(len(convert) <= 4):
                    count = 4 - len(convert)
                    convert = count * '0' + convert
                __all__ = __all__ + convert
                

    bitlaenge = len(__all__)
    anzahl_groups = bitlaenge / 8
    z = 0
    while z <= anzahl_groups-1:
        group = '0b' + __all__[z*8:(z+1)*8]
        z = z+1

        decimal = eval(group)
        last = chr(decimal)
        decode = decode + last
    print decode

if __name__ == "__main__":
    base16decode("")
    base16decode("66")
    base16decode("666F")
    base16decode("666F6F")
    base16decode("666F6F62")
    base16decode("666F6F6261")
    base16decode("666F6F626172")
